package client;

import java.io.IOException;
import java.util.ArrayList;
import GUI.MainFrame;
import ocsf.client.AbstractClient;

/**
 * Observable client
 * @author Benny
 *
 */
public class ObservableClient extends AbstractClient
{
	MainFrame mf;
	/**
	 * constructs a new Observable Client
	 * @param host given host 
	 * @param port given port
	 * @param M main frame of the program
	 * @throws IOException
	 */
    public ObservableClient(String host, int port, MainFrame M) throws IOException 
    {    
    		
            super(host, port);
            this.mf=M;
            openConnection();
    }

    /**
     * receive a message and direct it to the running server
     * @param msg message received 
     */
    public void handleMessageFromClientUI(ArrayList<Object> msg)
    {
        try
            {
        	openConnection();
            sendToServer(msg);
            }
            catch(IOException e)
            {
                System.out.println("Could not send message to server.  Terminating client.");
                quit();
            }       
    } 
    /**
     * receive a specific message message from the running server and direct it
     */
	public void handleMessageFromServer(Object msg) 
    {     
		mf.receiveMessage(msg);
		ArrayList<Object> result=(ArrayList<Object>) msg;
		System.out.println("recieved message from server, type: "+ result.get(0));
    }
    
    public void quit()
    {
	    try
	    {
	    	closeConnection();
	    }
	    catch(IOException e) {}
	    System.exit(0);
    }
}
